#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if (root == nullptr)return ret;
        queue<TreeNode*> q;
        q.push(root);
        int linesize;
        int count = 0;
        while (!q.empty())
        {
            linesize = q.size();
            vector<int> v;
            ret.push_back(v);
            while (linesize--)
            {
                TreeNode* x = q.front();
                if (x->left != nullptr)q.push(x->left);
                if (x->right != nullptr)q.push(x->right);
                ret[count].push_back(x->val);
                q.pop();
            }

            count++;
        }
        return ret;
    }
};